package com.slkj.service;

import com.slkj.controller.RouteController;
import com.slkj.dto.Meta;
import com.slkj.dto.UserRouteDTO;
import com.slkj.dto.UserRouteDTOList;
import com.slkj.dto.UserRouteDTOchildren;
import com.slkj.entity.Btn;
import com.slkj.entity.UserRoute;
import com.slkj.entity.Userinfo;
import com.slkj.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName RouteService.java
 * @Description TODO
 * @createTime 2020年03月12日 15:31:00
 */
@Service
public class RouteService {

    @Autowired
    private BtnMapper btnMapper;

    @Autowired
    private UserRouteMapper userRouteMapper;
    @Autowired
    private UserMapper userMapper;


    @Autowired
    private UserRoleHasBtnMapper userRoleHasBtnMapper;


    private Logger log = LoggerFactory.getLogger(RouteController.class);

    public UserRouteDTOList getRoute(String username) {
        Userinfo userinfo = userMapper.selectByUsername(username);
        List<Btn> btns = userRoleHasBtnMapper.selectByRoleId(userinfo.getRoleId());

        if (btns == null) {

        }

        if (btns.size() == 0) {
            throw new NullPointerException("无数据");
        }

        List<UserRoute> userRouteListChildrenList = userRouteMapper.selectChildrenBybtns(btns);
        Set<UserRouteDTO> userRouteDTOSet = new LinkedHashSet<>();

        List<UserRoute> userRouteListParentList = userRouteMapper.selectParentByid(userRouteListChildrenList);

        for (UserRoute route : userRouteListParentList) {
            UserRouteDTO userRouteDTO = new UserRouteDTO();
            boolean result = false;
            int i = 0;
            String redirect = "";
            List<UserRouteDTOchildren> userRouteDTOchildrenList = new ArrayList<>();

            for (UserRoute userRoute : userRouteListChildrenList) {
                if (route.getId().equals("" + userRoute.getpId())) {
                    if (userRoute.getHidden() == 0 && i == 0) {
                        redirect = "/" + userRoute.getPath();
                        i++;
                    }

                    UserRouteDTOchildren userRouteDTOchildren = new UserRouteDTOchildren();
                    userRouteDTOchildren.setId(userRoute.getId());
                    userRouteDTOchildren.setName(userRoute.getName());
                    userRouteDTOchildren.setPath(userRoute.getPath());
                    userRouteDTOchildren.setComponent(userRoute.getComponent());
                    Meta meta = new Meta();
                    meta.setTitle(userRoute.getTitle());
                    meta.setCloseable((userRoute.getCloseable() == 1 ? true : false));
                    meta.setHidden((userRoute.getHidden() == 1 ? true : false));
                    meta.setIcon(userRoute.getIcon());
                    userRouteDTOchildren.setMeta(meta);
                    userRouteDTOchildrenList.add(userRouteDTOchildren);
                    result = true;
                }


                userRouteDTO.setId(route.getId());
                userRouteDTO.setName(route.getName());
                userRouteDTO.setPath(route.getPath());
                userRouteDTO.setComponent(route.getComponent());
                Meta meta = new Meta();
                meta.setTitle(route.getTitle());
                meta.setCloseable((route.getCloseable() == 1 ? true : false));
                meta.setIcon(route.getIcon());
                userRouteDTO.setMeta(meta);


                if (result) {
                    userRouteDTO.setRedirect(route.getRedirect() + redirect);
                    userRouteDTO.setChildren(userRouteDTOchildrenList);
                }

                if (!userRouteDTOSet.contains(userRouteDTO)) {
                    userRouteDTOSet.add(userRouteDTO);
                }
            }

        }
//
//        List<UserRouteDTOchildren> userRouteDTOchildrenList2 = new ArrayList<>();
//
//        for (UserRoute userRoute : userRouteList) {
//
//
//            String redirect = "";
//            List<UserRoute> userRoutes = userRouteMapper.selectByPId(userRoute.getId());
//
//
//            if (userRoutes != null && userRoutes.size() != 0) {
//
//                for (UserRoute route : userRoutes) {
//
//                }
//            }
//
//
//        }
        List<String> btnList = new ArrayList<>();

//        Iterator<UserRouteDTO> iterator = userRouteDTOSet.iterator();
//        while (iterator.hasNext()) {
//            boolean result = false;
//            UserRouteDTO userRouteDTO = iterator.next();
//            for (UserRouteDTO routeDTO : userRouteDTOSet) {
//                if (userRouteDTO.getId().equals(routeDTO.getId())) {
//                    result = true;
//                }
//            }
//
//            if (result) {
//                iterator.remove();
//            }
//
//        }

        for (Btn btn : btns) {
            btnList.add(btn.getBtnCode());
        }
        UserRouteDTOList userRouteDTOList1 = new UserRouteDTOList();
        userRouteDTOList1.setBtnList(btnList);
        userRouteDTOList1.setUserRouteDTOSet(userRouteDTOSet);


        return userRouteDTOList1;
    }

    public List<UserRoute> getBtn() {
        List<UserRoute> userRouteList = userRouteMapper.selectParent();
        if (userRouteList == null) {
            throw new NullPointerException("空指针异常");
        }
        for (UserRoute userRoute : userRouteList) {
            List<Btn> btnList = btnMapper.selectByParentId(userRoute);
            userRoute.setBtnList(btnList);
        }

        return userRouteList;
    }


    public List<String> getBtnList(String roleId) {
        List<Btn> btnList = userRoleHasBtnMapper.selectByRoleId(roleId);
        if (btnList == null) {
            throw new NullPointerException("空指针异常");
        }
        List<String> btnListString = new ArrayList<>();
        for (Btn btn : btnList) {
            btnListString.add(btn.getId());
        }
        return btnListString;
    }

    public void updateRoute(String roleId, List<String> btnList) {
        userRoleHasBtnMapper.deleteBtnByRoleId(roleId);
        userRoleHasBtnMapper.insert(roleId, btnList);
    }
}
